<?php
namespace app\models;

use Yii;
use yii\base\Model;
use yii\web\UploadedFile;
use OSS\Core\OssException;

class UploadForm extends Model
{
    /**
     * @var UploadedFile
     */
    public $idfa_file;
    public $img_file;
    public $ajax_idfa;
    public $user_file;

    public function scenarios()
    {
        return [
            'upload-idfa' => ['idfa_file'],
            'upload-imgfile' => ['img_file'],
        ];
    }

    public function rules()
    {
        return [
            [['idfa_file'], 'file', 'skipOnEmpty' => false ,'on' => 'upload-idfa'],//, 'extensions' => 'csv'
            [['img_file'], 'file', 'skipOnEmpty' => true , 'extensions' => 'png, jpg, jpeg', 'on' => 'upload-imgfile'],
        ];
    }
    
    /**
     * @param $path 上传到/web/uploads/里指定文件夹
     * @param $field_name 文件上传表单字段名
     * @return string 上传成功返回文件名 上传失败返回
     */
    public function upload($field_name, $path = '')
    {
        if ($this->validate()) {
            $newpath = TASK_UPLOADS_PATH . $path;

            $new_file_name = Yii::$app->tool->getRandKey();
            $newfile = $newpath . $new_file_name . '.' . $this->$field_name->extension;

            $this->$field_name->saveAs($newfile);
            return $new_file_name . "." . $this->$field_name->extension;
        } else {
            return false;
        }
    }

    public function attributeLabels()
    {
        return [
            'img_file' => '上传图片',

        ];
    }

    /**
     * 上传
     */
    public function uploadFile($field_name, $path = '')
    {
        if ($this->validate()) {

            $newfile = OSS_CDN_IMG_SYS_PATH . '/' . Yii::$app->tool->getRandKey() . '.' . $this->$field_name->extension;

            $ret = UploadForm::uploadFileToOss($this->$field_name->tempName, $newfile);

            return OSS_CDN_HOST . '/' . $newfile;

        } else {
            return false;
        }
    }

    /**
     * 上传到alioss
     */
    public static function uploadFileToOss($file ,$object)
    {
        try {
            $ossClient = new \OSS\OssClient(OSS_ACCESS_ID, OSS_ACCESS_KEY, OSS_ENDPOINT_UPLOAD, true);
        } catch (OssException $e) {
            Yii::error('get_oss_client_error, msg:' . $e->getMessage());
            return false;
        }

        try {
            $file_result = $ossClient->uploadFile(OSS_BUCKET, $object , $file);


        } catch (OssException $e) {
            Yii::error('upload_to_oss_error, msg:' . $e->getMessage());
            return false;
        }

        return $file_result;
    }

    /**
     * 组装图片地址
     */
    public static function getFileUrl($path)
    {
        $url = '';

        if (empty($path)) {
            return $url;
        }

        if (strpos($path, 'http://') === false && strpos($path, 'https://') === false) { //如果是相对地址的
            $url = HOST_PATH . FILE_STORE_PATH . $path;
        } elseif (strpos($path, 'http://') !== false && strpos($path, 'https://') === false) { //如果是http协议的
            $url = str_replace('http://' . OSS_ENDPOINT_OLD, OSS_CDN_HOST, $path);
        } else { //其它情况
            $url = $path;
        }

        return $url;
    } 
}
?>
